﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Map.aspx.cs" Inherits="Besture.UI.Map" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>关键字查询</title>
<link rel="stylesheet" type="text/css" href="http://code.autonavi.com/Public/css/demo.Default.css" /> 
<script language="javascript" src="http://webapi.amap.com/maps?v=1.2&key=8461d1cc7d3947df8e640c562390516f"></script>
<script language="javascript">
    var mapObj;
    var marker = new Array();
    var windowsArr = new Array();
    //基本地图加载
    function mapInit() {
        mapObj = new AMap.Map("iCenter");
        var clickEventListener = AMap.event.addListener(mapObj, 'click', function (e) {
            opener.document.getElementById("jingdu").value = e.lnglat.getLng();
            opener.document.getElementById("weidu").value = e.lnglat.getLat();
        });
    }
    function placeSearch() {
        var MSearch;
        var keyword = document.getElementById('keyword').value;
        mapObj.plugin(["AMap.PlaceSearch"], function () {
            MSearch = new AMap.PlaceSearch({ //构造地点查询类
                pageSize: 10,
                pageIndex: 1,
                city: "022" //城市
            });
            AMap.event.addListener(MSearch, "complete", keywordSearch_CallBack); //返回地点查询结果
            MSearch.search(keyword); //关键字查询
        });
    }
    //添加marker&infowindow    
    function addmarker(i, d) {
        var lngX = d.location.getLng();
        var latY = d.location.getLat();
        var markerOption = {
            map: mapObj,
            icon: "http://webapi.amap.com/images/" + (i + 1) + ".png",
            position: new AMap.LngLat(lngX, latY)
        };
        var mar = new AMap.Marker(markerOption);
        marker.push(new AMap.LngLat(lngX, latY));

        var infoWindow = new AMap.InfoWindow({
            content: "<h3><font color=\"#00a6ac\">  " + (i + 1) + ". " + d.name + "</font></h3>" + TipContents(d.type, d.address, d.tel),
            size: new AMap.Size(300, 0),
            autoMove: true,
            offset: new AMap.Pixel(0, -30)
        });
        windowsArr.push(infoWindow);
        var aa = function (e) { infoWindow.open(mapObj, mar.getPosition()); };
        AMap.event.addListener(mar, "click", aa);
        var bb = function (e) {
            opener.document.getElementById("jingdu").value = e.lnglat.getLng();
            opener.document.getElementById("weidu").value = e.lnglat.getLat();
        };
        AMap.event.addListener(mar, "click", bb);
    }
    //回调函数
    function keywordSearch_CallBack(data) {
        //清空地图上的InfoWindow和Marker
        windowsArr = [];
        marker = [];
        mapObj.clearMap();
        var resultStr = "";
        var poiArr = data.poiList.pois;
        var resultCount = poiArr.length;
        for (var i = 0; i < resultCount; i++) {
            addmarker(i, poiArr[i]);
        }
        mapObj.setFitView();
        //document.getElementById("result").innerHTML = resultStr;
    }
    function TipContents(type, address, tel) {  //窗体内容
        if (type == "" || type == "undefined" || type == null || type == " undefined" || typeof type == "undefined") {
            type = "暂无";
        }
        if (address == "" || address == "undefined" || address == null || address == " undefined" || typeof address == "undefined") {
            address = "暂无";
        }
        if (tel == "" || tel == "undefined" || tel == null || tel == " undefined" || typeof address == "tel") {
            tel = "暂无";
        }
        var str = "  地址：" + address + "<br />  电话：" + tel + " <br />  类型：" + type;
        return str;
    }
    function openMarkerTipById1(pointid, thiss) {  //根据id 打开搜索结果点tip
        thiss.style.background = '#CAE1FF';
        windowsArr[pointid].open(mapObj, marker[pointid]);
    }
    function onmouseout_MarkerStyle(pointid, thiss) { //鼠标移开后点样式恢复
        thiss.style.background = "";
    }
</script>
</head>
<body onload="mapInit();">  
    <div id="iCenter" style="width: 99%"></div>
    <div class="demo_box">
        <p><input type='text' name='keyword' id='keyword' value=''/><input type="button" value="搜索" onclick="placeSearch()"/>
        </p>
    </div>        
</body>
</html>
